package com.houhanzhi.manager.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.houhanzhi.manager.ShortLinkManager;
import com.houhanzhi.mapper.ShortLinkMapper;
import com.houhanzhi.model.ShortLinkDO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class ShortLinkManagerImpl implements ShortLinkManager {

    @Autowired
    private ShortLinkMapper shortLinkMapper;

    @Override
    public int addShortLink(ShortLinkDO shortLinkDO) {
        return shortLinkMapper.insert(shortLinkDO);
    }

    @Override
    public ShortLinkDO findByShortLinkCode(String shortLinkCode) {
        return shortLinkMapper.selectOne(new QueryWrapper<ShortLinkDO>().eq("code", shortLinkCode));
    }

    @Override
    public int del(String shortLinkCode, Long accountNo) {
        return shortLinkMapper.update(null, new UpdateWrapper<ShortLinkDO>()
                .set("del", 1)
                .eq("code", shortLinkCode)
                .eq("account_no", accountNo));
    }

    /**
     * 更新短链
     * @param shortLinkDO
     * @return
     */
    @Override
    public int update(ShortLinkDO shortLinkDO) {
        return shortLinkMapper.update(null, new UpdateWrapper<ShortLinkDO>()
                .eq("code", shortLinkDO.getCode())
                .eq("account_no", shortLinkDO.getAccountNo())
                .eq("del", 0)
                .set("title", shortLinkDO.getTitle())
                .set("domain", shortLinkDO.getDomain())
        );
    }
}
